#include "selection.h"
#include "mainwindow.h"
#include "ui_selection.h"
#include <QString>
#include"cinemasystem.h"
#include "muinformation.h"
#include "qstandarditemmodel.h"
#include "qstyleditemdelegate.h"

static int hallnumber=1;

Selection::Selection(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Selection)
{
    ui->setupUi(this);

//    QStandardItemModel *model = new QStandardItemModel();


//    CinemaSystem cinemasystem("2023-10-17", "Sample Movie", 100.0);   //括号里的参数没用
//    cinemasystem.halls= cinemasystem.readAuditoriumsFromFile("D:\\QT_projects\\Login\\2.txt");//从这里读取场次信息
//    unsigned int total_hall_number = cinemasystem.halls.size();

//    model->setRowCount(int(total_hall_number));
//    model->setColumnCount(4);
//    model->setRowCount(total_hall_number);
//    //QString film_data = QString::fromStdString(cinemasystem.halls[0].movie["2000-01-01"].first);

//    for (int row = 0; row < total_hall_number; ++row) {
//        for (int col = 0; col < 3; ++col) {
//            QModelIndex index = model->index(row, col);
//            if(col == 0){
//                //QString film_date = QString::fromStdString(cinemasystem.halls[row].movie);//电影名
//                //model->setData(index,film_date);
//                std::string __filmtime = cinemasystem.halls[row].movie.begin()->first;
//                QString film_time = QString::fromStdString(__filmtime);//电影时间
//                model->setData(index,film_time);
//            }else if(col == 1){
//                std::string __filmtime = cinemasystem.halls[row].movie.begin()->first;
//                QString film_name = QString::fromStdString(cinemasystem.halls[row].movie[__filmtime].first);//电影名
//                model->setData(index,film_name);
//            }else if(col == 2){
//                std::string __filmtime = cinemasystem.halls[row].movie.begin()->first;
//                std::string _prise = std::to_string(cinemasystem.halls[row].movie[__filmtime].second);
//                QString film_prise = QString::fromStdString(_prise);//价格
//                model->setData(index,film_prise);
//            }else if(col == 3){
//                QPushButton* button = new QPushButton("跳转");
//               button->setProperty("row", row); // 保存按钮所在行的索引
//               model->setData(index, QVariant::fromValue(button), Qt::UserRole+1);
//               connect(button, &QPushButton::clicked, [=]() {
//               // 获取按钮所在行的索引
//               int buttonRow = button->property("row").toInt();

//               // 执行窗口跳转逻辑，根据需要进行修改
//               // ...
//           });
//            }
//        }
//    }
//    ui->tableView->horizontalHeader()->resizeSection(0, 225);
//    //ui->tableView->columnWidth(0);
//    ui->tableView->setModel(model);

    QStandardItemModel *model = new QStandardItemModel();

    CinemaSystem cinemasystem("2023-10-17", "Sample Movie", 100.0);   //括号里的参数没用
    cinemasystem.halls= cinemasystem.readAuditoriumsFromFile("D:\\QT_projects\\Login\\2.txt");//从这里读取场次信息
    unsigned int total_hall_number = cinemasystem.halls.size();

    model->setRowCount(int(total_hall_number));
    model->setColumnCount(4);
    model->setRowCount(total_hall_number);

    for (int row = 0; row < total_hall_number; ++row) {
        for (int col = 0; col < 3; ++col) {
            QModelIndex index = model->index(row, col);
            if(col == 0){
                std::string __filmtime = cinemasystem.halls[row].movie.begin()->first;
                QString film_time = QString::fromStdString(__filmtime);//电影时间
                model->setData(index,film_time);
            }else if(col == 1){
                std::string __filmtime = cinemasystem.halls[row].movie.begin()->first;
                QString film_name = QString::fromStdString(cinemasystem.halls[row].movie[__filmtime].first);//电影名
                model->setData(index,film_name);
            }else if(col == 2){
                std::string __filmtime = cinemasystem.halls[row].movie.begin()->first;
                std::string _prise = std::to_string(cinemasystem.halls[row].movie[__filmtime].second);
                QString film_prise = QString::fromStdString(_prise);//价格
                model->setData(index,film_prise);
            }
        }
    }

    // 创建自定义的委托
    class ButtonDelegate : public QStyledItemDelegate {
    public:
        ButtonDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {}

        void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override {
            QStyledItemDelegate::paint(painter, option, index);
        }

        QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override {
            if (index.column() == 3) {
                QPushButton *button = new QPushButton("跳转", parent);
                connect(button, &QPushButton::clicked, [=]() {
                    // 获取按钮所在行的索引
                    int buttonRow = index.row();
                    MainWindow *mainwindow = new MainWindow(parent,buttonRow+1);
                    mainwindow->show();

                    // 执行窗口跳转逻辑，根据需要进行修改
                    // ...
                });
                return button;
            }
            return QStyledItemDelegate::createEditor(parent, option, index);
        }

        void setEditorData(QWidget *editor, const QModelIndex &index) const override {
            QStyledItemDelegate::setEditorData(editor, index);
        }

        void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override {
            QStyledItemDelegate::setModelData(editor, model, index);
        }
    };

    // 设置自定义的委托
    ui->tableView->setItemDelegateForColumn(3, new ButtonDelegate(ui->tableView));

    ui->tableView->horizontalHeader()->resizeSection(0, 225);
       //ui->tableView->columnWidth(0);
    ui->tableView->setModel(model);


}

Selection::~Selection()
{
    delete ui;
}


void Selection::on___myinfo_clicked()
{
        MuInformation *myinfo = new MuInformation(this);
        myinfo->show();
}

int return_hall_number(){
    return hallnumber;
}
